const path = require('path')
const utils = require('./utils')
const config = require('./config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve(dir) {
  return path.join(__dirname, '..', dir)
}

function resolveSrc(dir) {
  return path.join(__dirname, '..', 'src', dir)
}

module.exports = {
  entry: {
    app: ['babel-polyfill', resolveSrc('app.js')],
  },
  output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].js'),
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      '@assets': resolveSrc('assets'),
      '@modules': resolveSrc('modules'),
      '@utils': resolveSrc('utils'),
      '@views': resolveSrc('views-html'),
    }
  },
  // externals: {
  //   'vue': 'Vue',
  //   'vue-router': 'VueRouter',
  //   'element-ui': 'ElementUI',
  //   'vipspa': 'vipspa',
  // },
  module: {
    rules: [{
      test: /\.html$/,
      use: ['html-loader'],
    }, {
      test: /\.vue$/,
      use: [{
        loader: 'vue-loader',
        options: vueLoaderConfig
      }]
    }, {
      test: /\.js$/,
      include: [resolve('src'), resolve('test')],
      use: [{
        loader: 'babel-loader',
        options: {
          presets: ['env'],
          compact: false,
        },
      }]
    }, {
      test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
      use: [{
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      }]
    }, {
      test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
      use: [{
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }]
    }, {
      test: /\.(mp3|aac|flac)(\?.*)?$/,
      use: [{
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('audio/[name].[hash:7].[ext]')
        }
      }]
    }, {
      test: /\.less$/,
      use: [{
        loader: 'style-loader!css-loader!less-loader'
      }]
    }]
  }
}